Graph Traversal: Breadth-First Search (BFS)

PolyU DSAI2201 Lecture 13 2025-11-25

Algorithm: Breadth-First Search (BFS)

BFS is a fundamental graph traversal algorithm used to explore a graph G=(V,E)G=(V, E) systematically, finding the shortest path (in terms of edges) from a source node ss to all other reachable nodes. It guarantees nodes are processed in increasing order of distance.

BFS Mechanism

  1. Initialization: For every node vv, set `color` to `WHITE` (unvisited), distance d[v]=d[v] = \infty, and parent π[v]=NIL\pi[v] = \text{NIL}.
  2. Source Setup: Set the source ss to `GRAY` (discovered), d[s]=0d[s]=0, and initialize a FIFO Queue QQ with ss.
  3. Exploration Loop: While QQ is not empty, dequeue node uu:
    • For every neighbor vv of uu:
      • If vv is `WHITE`:
        • Set vv to `GRAY`.
        • d[v]=d[u]+1d[v] = d[u] + 1 (Level increment).
        • π[v]=u\pi[v] = u (Record parent).
        • Enqueue vv.
    • Once all neighbors are processed, mark uu as `BLACK` (fully explored).

Key BFS Properties & Use Cases

The Power of the Queue: BFS fundamentally relies on the FIFO property of the Queue. This ensures that the algorithm visits all nodes at distance kk from the source before visiting any nodes at distance k+1k+1. This level-by-level exploration guarantees unweighted shortest paths.

Connectivity and Levels:

  1. Levels: The value d[v]d[v] represents the minimum number of edges needed to reach vv from ss. This is the shortest path length.
  2. BFS Tree: The set of parent pointers π\pi implicitly defines the BFS Tree, which is a subgraph containing only shortest-path edges from ss.
  3. Use Cases: Finding shortest paths (unweighted), networking broadcast, web crawlers (finding minimum link depth), and garbage collection (reachable objects).

BFS Complexity Analysis

BFS is highly efficient, examining every vertex and edge exactly once, provided the graph is represented using an Adjacency List.

MetricCost (Adjacency List)Explanation
InitializationO(V)O(|V|)Processing all nn vertices once.
Queue OperationsO(V)O(|V|)Each vertex is enqueued and dequeued exactly once.
Edge ExaminationO(E)O(|E|)The total time spent scanning adjacency lists is proportional to the number of edges mm.
Total RuntimeO(V+E)=O(n+m)O(|V| + |E|) = O(n+m)Linear time complexity relative to the size of the graph.
📝 Interactive Quiz

1. Which data structure is essential for implementing the level-by-level exploration of Breadth-First Search?

  • A) Stack
  • B) Queue
  • C) Priority Queue
  • D) Hash Table

2. What is the time complexity of BFS on a graph with nn vertices and mm edges, when using an adjacency list?

  • A) O(n2)O(n^2)
  • B) O(mlogn)O(m \log n)
  • C) O(n+m)O(n+m)
  • D) O(nlogm)O(n \log m)

3. BFS is guaranteed to find the shortest path in which type of graph?

  • A) Weighted graphs
  • B) Unweighted graphs
  • C) Directed acyclic graphs (DAGs) only
  • D) All graphs

4. After running BFS from a source ss, which outputs are needed to reconstruct the shortest paths and distances?

  • A) The set of all edges EE in the graph.
  • B) The Distance Array dd and the Parent Array π\pi.
  • C) The input Adjacency Matrix AA.
  • D) A set of priority weights ww.